/*!
 * \file frlist_show.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __my_list_node_free(Frlist_node *node)
{
    frfree(node->data);
}

static void __my_list_node_show(Frlist_node* node, char *p_outbuff, unsigned int ui_buff_size)
{
    snprintf(p_outbuff,ui_buff_size,"%s",(char *)node->data);
}

static void __case1(void)
{
    Frlist_header *list = NULL;
    char *p_outbuff = NULL;
    p_outbuff = (char *)frmalloc(8);
    memset(p_outbuff,0x00,8);

    frlist_show(list, p_outbuff, 8, __my_list_node_show);

    CU_ASSERT_STRING_EQUAL(p_outbuff,"\0");

    frfree(p_outbuff);

}

static void __case2(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_outbuff = NULL;
    p_outbuff = (char *)frmalloc(8);
    memset(p_outbuff,'\0',8);

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","h");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","e");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","o");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show(list, p_outbuff, 8, NULL);

    CU_ASSERT_STRING_EQUAL(p_outbuff,"\0");

    frfree(p_outbuff);
    frlist_destroy(list,__my_list_node_free);

}

static void __case3(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_outbuff = NULL;
    p_outbuff = (char *)frmalloc(1);
    memset(p_outbuff,'\0',1);

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","h");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","e");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","o");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show(list, p_outbuff, 1, __my_list_node_show);

    CU_ASSERT_STRING_EQUAL(p_outbuff,"\0");

    frfree(p_outbuff);
    frlist_destroy(list,__my_list_node_free);

}

static void __case4(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_outbuff = NULL;
    p_outbuff = (char *)frmalloc(3);
    memset(p_outbuff,'\0',3);

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","h");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","e");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","o");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show(list, p_outbuff, 3, __my_list_node_show);

    CU_ASSERT_STRING_EQUAL(p_outbuff, "he");

    frfree(p_outbuff);
    frlist_destroy(list,__my_list_node_free);

}

static void __case5(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_outbuff = NULL;
    p_outbuff = (char *)frmalloc(8);
    memset(p_outbuff,'\0',8);

    list = frlist_create();

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","h");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","e");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","l");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","o");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show(list, p_outbuff, 8, __my_list_node_show);

    CU_ASSERT_STRING_EQUAL(p_outbuff, "hello");

    frfree(p_outbuff);
    frlist_destroy(list,__my_list_node_free);

}

int add_suite_frlist_show(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frlist frlist_show()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
